package com.boy.rpc.framework.console.dao;


import com.boy.rpc.framework.console.Constant;
import com.boy.rpc.framework.console.bean.RValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

import java.util.*;


@Service
public class RedisDao {

	private static final Logger logger = LoggerFactory.getLogger(RedisDao.class);

	private static final JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
	
	@Autowired
	RedisTemplateFactory redisTemplateFactory;

	/**
	 * 设置key-value
	 * @param serverName
	 * @param dbIndex
	 * @param key
     * @param values
     */
	public void addKV(String serverName, int dbIndex, String key,
			String[] values) {
		logger.debug("redis addKV key:"+key+", value:"+values);
		RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName);
		redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
		Constant.redisConnectionDbIndex.set(dbIndex);
		redisTemplate.opsForValue().set(key,values);
	}

	/**
	 * 获取key-value
	 * @param serverName
	 * @param dbIndex
	 * @param key
     * @return
     */
	public Object getKV(String serverName, int dbIndex, String key) {
		logger.debug("--redis--getKV---key:"+key);
		RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName);
		Constant.redisConnectionDbIndex.set(dbIndex);
		//序列化
		redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
		final Object value = redisTemplate.opsForValue().get(key);
		logger.debug("--redis--getKV---value:"+value);
		List list = new ArrayList();
		list.add(new RValue(value));
		logger.debug("--redis--getKV---result:"+list);
		return list;
	}

	/**
	 * 删除redis中的key
	 * @param serverName
	 * @param dbIndex
	 * @param deleteKeys
     */
	public void delKeys(String serverName, int dbIndex, String deleteKeys) {
		logger.debug("delRedisKeys --->"+deleteKeys);
		RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName);
		Constant.redisConnectionDbIndex.set(dbIndex);
		String[] keys = deleteKeys.split(",");
		redisTemplate.delete(Arrays.asList(keys));
	}

	/**
	 * 根据key删除值
	 * @param serverName
	 * @param dbIndex
	 * @param key
     */
	public void delValue(String serverName, int dbIndex, String key) {
		logger.debug("delValue by key:"+key);
		RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName);
		Constant.redisConnectionDbIndex.set(dbIndex);
		redisTemplate.opsForValue().set(key, "");
	}

	/**
	 * 根据key更新值
	 * @param serverName
	 * @param dbIndex
	 * @param key
     * @param value
     */
	public void updateValue(String serverName, int dbIndex, String key, String value) {
		logger.debug("updateValue by key:"+key+",value:"+value);
		RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName);
		redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
		Constant.redisConnectionDbIndex.set(dbIndex);
		redisTemplate.opsForValue().set(key, value);
	}
}
